<?php

class Browse_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}
	
	function get_categories()
	{
		$sql = "SELECT * FROM category";
		$query = $this->db->query($sql);
		
		$data = array();
		if ($query->num_rows() > 0)
		{
			foreach($query->result_array() as $row)
			{
				$data[] = $row;
			}
		}
		
		return $data;
	}
	
	function search($fb_id, $criteria='', $catid='')
	{
		if (array_key_exists('page', $criteria))
			$page = (($criteria['page'] - 1) * 16);
		else
			$page = '';
		$extra = $this->insert_criteria($criteria, $catid);
		if ($criteria && array_key_exists('sort_by',$criteria) && $criteria['sort_by'] == 'pricelowtohigh')
		{
			$query = "SELECT * FROM offer AS o WHERE o.status <> 'Closed'";
			$query .= $extra;
			$query .= " ORDER BY o.item_price ASC";
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			$result = $this->db->query($query);
			$data = array();
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					$data[] = $row;
				}
			}
		}
		else if ($criteria && array_key_exists('sort_by',$criteria) && $criteria['sort_by'] == 'pricehightolow')
		{
			$query = "SELECT * FROM offer AS o WHERE o.status <> 'Closed'";
			$query .= $extra;
			$query .= " ORDER BY o.item_price DESC";
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			$result = $this->db->query($query);
			$data = array();
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					$data[] = $row;
				}
			}
		}
		else if ($criteria && array_key_exists('sort_by',$criteria) && $criteria['sort_by'] == 'newestfirst')
		{
			$query = "SELECT * FROM offer AS o WHERE o.status <> 'Closed'";
			$query .= $extra;
			$query .= " ORDER BY o.date_created DESC";
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			$result = $this->db->query($query);
			$data = array();
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					$data[] = $row;
				}
			}
		}
		else if ($criteria && array_key_exists('sort_by',$criteria) && $criteria['sort_by'] == 'oldestfirst')
		{
			$query = "SELECT * FROM offer AS o WHERE o.status <> 'Closed'";
			$query .= $extra;
			$query .= " ORDER BY o.date_created ASC";
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			$result = $this->db->query($query);
			$data = array();
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					$data[] = $row;
				}
			}
		}
		else
		{
			$id_array = array();
			$query = $this->db->query("SELECT category_id FROM category_preference WHERE fb_id = ?", $fb_id); // get category preferences
			$pre_array = array();
			if ($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
					$pre_array[] = $row['category_id'];
			}
			$preference = implode("','",$pre_array);
			
			$query = $this->db->query("SELECT area_id FROM area_preference WHERE fb_id = ?", $fb_id); // get area preferences too
			$area_array = array();
			if ($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
					$area_array[] = $row['area_id'];
			}
			$area = implode("','",$area_array);
			
			// first grab all offers that satisfy both category and area preferences
			$query = "SELECT * FROM offer AS o 
					JOIN area_preference AS a ON o.fb_id = a.fb_id 
					WHERE o.status <> 'Closed' 
					AND o.category_id IN ('".$preference."') 
					AND a.area_id IN ('".$area."')";
			
			$query .= $extra;
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			
			$result = $this->db->query($query);
			$data = array();
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					if (!in_array($row['offer_id'], $id_array))
					{
						$data[] = $row;
						$id_array[] = $row['offer_id'];
					}
				}
			}
			
			// next grab all offers that satisfy only category preferences
			$query = "SELECT * FROM offer AS o WHERE o.status <> 'Closed' AND o.category_id IN ('".$preference."')";
			
			$query .= $extra;
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			
			$result = $this->db->query($query);
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					if (!in_array($row['offer_id'], $id_array))
					{
						$data[] = $row;
						$id_array[] = $row['offer_id'];
					}
				}
			}
			
			// next grab all offers that satisfy only area preferences
			$query = "SELECT * FROM offer AS o
						JOIN area_preference AS a ON o.fb_id = a.fb_id
						WHERE o.status <> 'Closed' 
						AND a.area_id IN ('".$area."')";
						
			$query .= $extra;
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			
			$result = $this->db->query($query);
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					if (!in_array($row['offer_id'], $id_array))
					{
						$data[] = $row;
						$id_array[] = $row['offer_id'];
					}
				}
			}
			
			// finally grab the rest that doesn't fit into criteria
			$query = "SELECT * FROM offer AS o WHERE o.status <> 'Closed'";
			
			$query .= $extra;
			if ($page)
				$query .= " LIMIT " . $page . ", 16";
			
			$result = $this->db->query($query);
			if ($result->num_rows() > 0)
			{
				foreach($result->result_array() as $row)
				{
					if (!in_array($row['offer_id'], $id_array))
					{
						$data[] = $row;
						$id_array[] = $row['offer_id'];
					}
				}
			}
		}
		
		return $data;
	}
	
	function insert_criteria($criteria, $catid)
	{
		$query = ' ';
		if ($catid)
			$query .= "AND o.category_id = ".$catid;
		if ($criteria) //add in criteria
		{
			if (array_key_exists('category', $criteria) && $criteria['category']!=0)
				$query .= " AND o.category_id = ".$criteria['category'];
			
			if (array_key_exists('show', $criteria))
			{
				if($criteria['show'] == 'reserved')
					$query .= " AND o.status = 'Reserved'";
				if($criteria['show'] == 'available')
					$query .= " AND o.status = 'Available'";
			}
			
			if (array_key_exists('price', $criteria))
			{
				if ($criteria['price'] == 'free')
					$query .= " AND o.item_price = '0.00'";
				else if ($criteria['price'] == 'cost')
					$query .= " AND o.item_price <> '0.00'";
			}
				
			if (array_key_exists('search', $criteria) && $criteria['search']!='Search' && $criteria['search']!='')
				$query .= " AND (LOWER(o.item_title) LIKE LOWER('%".$this->db->escape_like_str($criteria['search'])."%') OR LOWER(o.item_desc) LIKE LOWER('%".$this->db->escape_like_str($criteria['search'])."%'))";
			
		}
		return $query;
	}
	
}
	
?>